#include<iostream>
#include<cstring>
using namespace std;
class MyMatrix
{
private:
    int n;
    int** num;
public:
    MyMatrix(int _n)
    {
        n = _n;
        num = new int* [n];
        for (int i = 0; i < n; ++i)
        {
            num[i] = new int[n];
        }
        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < n; ++j)
            {
                num[i][j] = 0;
            }
        }
    }
    MyMatrix() {}
    void Set(int _n)
    {
        n = _n;
        num = new int* [n];
        for (int i = 0; i < n; ++i)
        {
            num[i] = new int[n];
        }
    }
    void ReadNum()
    {
        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < n; ++j)
            {
                int nu;
                cin >> nu;
                num[i][j] = nu;
            }
        }
    }
    MyMatrix(const MyMatrix& a)
    {
        n = a.n;
        num = new int* [n];
        for (int i = 0; i < n; ++i)
        {
            num[i] = new int[n];
        }
        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < n; ++j)
            {
                num[i][j] = a.num[i][j];
            }
        }
    }
    MyMatrix TT()
    {
        MyMatrix tmp(n);
        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < n; ++j)
            {
                tmp.num[i][j] = num[j][i];
            }
        }
        return tmp;
    }
    MyMatrix operator*(MyMatrix& b)
    {
        MyMatrix ans (b.TT());
        MyMatrix AAn(n);
        int cc = 0, ll = 0;
        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < n; ++j)
            {
                int sum = 0;
                for (int k = 0; k < n; ++k)
                {
                    sum = sum + ans.num[j][k] * num[i][k];
                }
                AAn.num[cc][ll] = sum;
                ll++;
                if (ll >= n)
                {
                    cc++;
                    ll = 0;
                }
            }
        }
        return AAn;
    }
    void show()
    {
        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < n; ++j)
            {
                if (j != 0)
                    cout << " ";
                cout << num[i][j];
            }
            cout << endl;
        }
    }
    MyMatrix operator=(const MyMatrix& a)
    {
        //MyMatrix ans(a.n);
        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < n; ++j)
            {
                num[i][j] = a.num[i][j];
            }
        }
        return *this;
        //return ans;
    }
    ~MyMatrix()
    {
        for (int i = n - 1; i >= 0; --i)
        {
            delete num[i];
        }
    }
};
int main()
{
    int c;
    cin >> c;
    int n;
    cin >> n;
    MyMatrix* arr=new MyMatrix[c];
    for (int i = 0; i < c; ++i)
    {
        arr[i].Set(n);
        arr[i].ReadNum();
    }
    MyMatrix ans(arr[0]);
    for (int i = 1; i < c; ++i)
    {
        MyMatrix tmp = ans * arr[i];
        ans=tmp;
    }
    ans.show();
    delete[] arr;
}